/**
 * 閰嶅悎 IOS 浣跨敤鏃剁殑鍒濆鍖栨柟娉�
 */
(function (win, doc) {
    var u = navigator.userAgent
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1

    /**
     * 閰嶅悎 IOS 浣跨敤鏃剁殑鍒濆鍖栨柟娉�
     */
    function iosFunction(callback) {
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge) // eslint-disable-line
        }
        if (window.WVJBCallbacks) {
            return window.WVJBCallbacks.push(callback)
        }
        window.WVJBCallbacks = [callback]
        var WVJBIframe = document.createElement('iframe')
        WVJBIframe.style.display = 'none'
        WVJBIframe.src = 'https://__bridge_loaded__'
        document.documentElement.appendChild(WVJBIframe)
        setTimeout(function () {
            document.documentElement.removeChild(WVJBIframe)
        }, 0)
    }

    function androidFunction(callback) {
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge) // eslint-disable-line
        }
        if (window.WVJBCallbacks) {
            return window.WVJBCallbacks.push(callback)
        }
        window.WVJBCallbacks = [callback]
        var WVJBIframe = document.createElement('iframe')
        WVJBIframe.style.display = 'none'
        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'
        document.documentElement.appendChild(WVJBIframe)
        setTimeout(function () {
            document.documentElement.removeChild(WVJBIframe)
        }, 0)
    }

    // !isAndroid && (window.setupWebViewJavascriptBridge = iosFunction);
    window.setupWebViewJavascriptBridge = isAndroid ? androidFunction : iosFunction
    // window.setupWebViewJavascriptBridge = setupWebViewJavascriptBridge;

    function CztvApi(debug = false) {
        console.log('CztvApi v1.0.8')
        this.debug = debug
    }

    /**
     * @description: 鍞よ捣鐧诲綍椤碉紝鏈櫥褰曞垯杩斿洖''锛岀櫥褰曟垚鍔焎ode: 200锛屽彇娑堢櫥褰昪ode: 401
     * @param {*} resCallback
     * @return {*}
     */
    CztvApi.prototype.login = function (resCallback) {
        const debug = this.debug
        if (debug) console.log('pushLoginPage')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.pushLoginPage', null, function (res) {
                if (debug) console.log('鐧诲綍锛�', res)
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * @description: 浠呰幏鍙栫敤鎴蜂俊鎭紝鍙敤鏉ュ垽鏂槸鍚﹀凡鐧诲綍锛屾湭鐧诲綍鍒欒繑鍥�''
     * @param {*} resCallback
     * @return {*} 
     */
    CztvApi.prototype.userInfo = function (resCallback) {
        const debug = this.debug
        if (debug) console.log('userInfo')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.userInfo', null, function (res) {
                if (debug) console.log('userInfo:', res)
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }


    /**
     * @description: 鍏抽棴椤甸潰
     * @param {*} resCallback
     * @return {*} 
     */
    CztvApi.prototype.close = function (resCallback) {
        const debug = this.debug
        if (debug) console.log('close')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.close', null, function (res) {
                if (debug) console.log('close:', res);
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 璺敱璺宠浆鏂规硶
     * @param {*} route 
     * @param {*} resCallback 
     */
    CztvApi.prototype.route = function (route, resCallback) {
        const debug = this.debug
        if (debug) console.log('cztvRoute')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.route', { route: route }, function (res) {
                if (debug) console.log('cztvRoute:', res);
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 淇敼鍒嗕韩鍙傛暟
     * @param {*} shareData 
     */
    CztvApi.prototype.changeShareInfo = function (shareData) {
        const debug = this.debug
        if (debug) console.log('changeShareInfo:', shareData)
        shareData != null && window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.changeShareInfo', shareData)
        })
    }

    /**
     * h5璋冭捣瀹㈡埛绔垎浜�
     * @param {*} shareData:(object) 鍒嗕韩鍙傛暟
     *    "url": 'http://mhudong.cztv.com/260366', //鍒嗕韩鐨勭綉鍧€閾炬帴
     *    "title": "123", //鍒嗕韩鐨勬爣棰�
     *    "content": "213", // 鍒嗕韩鐨勬枃瀛�
     *    "img": "https://ohudong.cztv.com/1/260366/images/qrcode.png", // 鍒嗕韩鐨勫浘鐗嘦rl
     * @param {*} resCallback
     */
    CztvApi.prototype.share = function (shareData, resCallback) {
        const debug = this.debug
        if (debug) console.log('share:', shareData)
        shareData != null && window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.share', shareData, function (res) {
                if (debug) console.log('share:', res);
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 鏄剧ず鍜岄殣钘忓垎浜寜閽�
     * @param {*} showShareButton
     */
    CztvApi.prototype.showShareButton = function (showShareButton = true) {
        const debug = this.debug
        if (debug) console.log('showShareButton:', showShareButton)
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.showShareButton', showShareButton)
        })
    }

    /**
     * 鑾峰彇绯荤粺淇℃伅
     * @param {*} resCallback
     */
    CztvApi.prototype.getSystemInfo = function (resCallback) {
        const debug = this.debug
        if (debug) console.log('getSystemInfo')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.getSystemInfo', null, function (res) {
                if (debug) console.log('getSystemInfo:', res);
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 淇濆瓨鍥剧墖
     * @param {*} img
     */
    CztvApi.prototype.saveImg = function (img) {
        if (!img) return
        const debug = this.debug
        if (debug) console.log('saveImg')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.saveImg', img)
        })
    }

    /**
     * 鐩存帴璋冭捣鍒嗕韩锛堜笉寮硅捣闈㈡澘锛�
     * @param {*} shareData
     * @param {*} resCallback
     */
    CztvApi.prototype.shareNoUi = function (shareData, resCallback) {
        if (!shareData || !shareData.type || !shareData.platform) return
        const debug = this.debug
        if (debug) console.log('shareNoUi')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.shareNoUi', shareData, function (res) {
                if (debug) console.log('shareNoUi:', res);
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 璋冭捣app椤甸潰
     * @param {*} openPagedata:(object) 璺宠浆鍙傛暟 鈥渞esource_type鈥�: 1 //璺宠浆绫诲瀷 鈥渞esource_body鈥濓細鈥漣d鈥� //椤甸潰鍔犺浇鎵€鐢ㄥ埌鐨刬d
     * @param {*} resCallback
     */
    CztvApi.prototype.openPage = function (openPagedata, resCallback) {
        const debug = this.debug
        if (debug) console.log('openPage')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.openPage', openPagedata, function (res) {
                console.log('璋冭捣app椤甸潰' + JSON.stringify(res));
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 閫氱煡app 褰撳墠h5椤甸潰淇℃伅
     * @param {*} pageInfo:(object) 
     *  "name": "audioSubject" //椤甸潰鍚嶇О
     * @param {*} resCallback
     */
    CztvApi.prototype.setPageInfo = function (pageInfo, resCallback) {
        const debug = this.debug
        if (debug) console.log('setPageInfo')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.setPageInfo', pageInfo, function (res) {
                console.log('setPageInfo complete');
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 鏀粯
     * @param {*} orderInfo:(object) 
     *  "orderNo": '' // 璁㈠崟鍙�
     *  "orderType": '' // 涓氬姟绫诲瀷
     * @param {*} resCallback
     */
    CztvApi.prototype.pay = function (orderInfo, resCallback) {
        const debug = this.debug
        if (debug) console.log('pay')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.pay', orderInfo, function (res) {
                console.log('pay complete');
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 鑾峰彇鐗堟湰鍙�
     * @param {*} resCallback
     */
    CztvApi.prototype.version = function (resCallback) {
        const debug = this.debug
        if (debug) console.log('version')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.version', null, function (res) {
                console.log('version complete');
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 淇敼鏍囬
     * @param {*} title:(string) 鏍囬鍚嶇О
     */
    CztvApi.prototype.refreshTitle = function (title) {
        const debug = this.debug
        if (debug) console.log('refreshTitle')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.refreshTitle', title)
        })
    }

    /**
     * 閫氱煡app 褰撳墠h5椤甸潰淇℃伅
     * @param {*} apiList:(object) 
     *  "apiList": ['CZTVJsdk.showShareButton', 'CZTVJsdk.share']
     * @param {*} resCallback
     */
    CztvApi.prototype.checkJsApi = function (apiList, resCallback) {
        const debug = this.debug
        if (debug) console.log('checkJsApi')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.checkJsApi', apiList, function (res) {
                if (debug) console.log('checkJsApi:', res);
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    /**
     * 鍋氫换鍔�
     * @param {*} zgltvDoTaskdata 
     *  "taskId": '' // 浠诲姟id
     *  "taskType": '' // 浠诲姟绫诲瀷 1锛氭祻瑙堜换鍔� 2锛氬鎴风VIP璐拱浠诲姟 3锛欻5灏忔父鎴�  4锛欻5鍟嗗煄璐拱
     *  "resource_type": '' // 瀹㈡埛绔烦杞被鍨�
     *  "resource_body": '' // taskType 3鎴栬€�4锛屽苟涓旀槸H5閾炬帴鐨勮瘽锛屽墠绔嚜琛岃В鏋愰摼鎺ュ悗闈㈢殑鍙傛暟锛堝弬鏁板悗鍙伴厤缃級
     * @param {*} resCallback 
     */
    CztvApi.prototype.zgltvDoTask = function (zgltvDoTaskdata, resCallback) {
        const debug = this.debug
        if (debug) console.log('zgltvDoTask')
        window.setupWebViewJavascriptBridge(function (bridge) {
            bridge.callHandler('CZTVJsdk.zgltvDoTask', zgltvDoTaskdata, function (res) {
                if (debug) console.log('zgltvDoTask:', res);
                if (resCallback) {
                    resCallback(res)
                }
            })
        })
    }

    win.CztvApi = CztvApi
})(window, document)